package com.zhongkun.controller;

import com.zhongkun.pojo.User;
import com.zhongkun.service.FileMapperService;
import com.zhongkun.service.UserMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    private UserMapperService userMapperService;

    @RequestMapping("/01")
    public ModelAndView display(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/loginAndRegeister");
        return modelAndView;
    }

    @RequestMapping("/login")
    public ModelAndView login(String username,String password){
        ModelAndView modelAndView = new ModelAndView();
        session = request.getSession();

        //获取所有用户 时间倒序
        List<User> users = userMapperService.findAllUserByTime();
        System.out.println("查询到所有用户:\n"+users);
        session.setAttribute("allUserByTime",users);

        //获取登录对象
        User userLogin = new User();
        userLogin.setUsername(username);
        userLogin.setPassword(password);
        System.out.println("传过来的用户名和密码分别是:"+username+","+password);

        //登录判断
        if(userMapperService.login(userLogin) != null){
            userLogin = userMapperService.login(userLogin);
            System.out.println(userLogin);
            //登录成功后传入完整user信息
            session.setAttribute("userLogin",userLogin);
            return new ModelAndView("redirect:/file/mainAfterLogin");
        }else{
            modelAndView.setViewName("/loginAndRegeister");
        }
        return modelAndView;
    }
}
